package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.MerchantStoreService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.MerchantStoreDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 谢洛涛
 * @date 2021/3/31 11:02
 */
@RestController
@RequestMapping("/api/merchantStore")
public class MerchantStoreController extends BaseController {
    @Resource
    private MerchantStoreService merchantStoreService;
    @Resource
    private UserService userService;

    @GetMapping("/findAllMerchantStoreByUserInfo")
    public ServerResponse findAllMerchantStoreByUserInfo(HttpServletRequest request, MerchantStoreDto merchantStoreDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.findAllMerchantStoreByUserInfo(merchantStoreDto);
    }

    @GetMapping("/findMerchantStoreListByUserId")
    public ServerResponse findMerchantStoreListByUserId(HttpServletRequest request, MerchantStoreDto merchantStoreDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantStoreService.findMerchantStoreListByUserId(merchantStoreDto));
    }

    @PutMapping("/updateIsExchange")
    public ServerResponse updateIsExchange(HttpServletRequest request, @RequestBody MerchantStoreDto merchantStoreDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if (StringUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage("获取商户信息失败");
        }
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.updateIsExchange(merchantStoreDto);
    }

    @PutMapping("/batchUpdateIsExchange")
    public ServerResponse batchUpdateIsExchange(HttpServletRequest request, @RequestBody MerchantStoreDto merchantStoreDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if (StringUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage("获取商户信息失败");
        }
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.batchUpdateIsExchange(merchantStoreDto);
    }

    @PostMapping("/list")
    public ServerResponse findMerchantStoreList(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        merchantStoreDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
        merchantStoreDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        return ServerResponse.createBySuccess(merchantStoreService.findMerchantStoreList(merchantStoreDto));
    }

    @PutMapping
    public ServerResponse updateMerchantStore(HttpServletRequest request, @RequestBody MerchantStoreDto merchantStoreDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        if (RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId())) {
            return merchantStoreService.updateMerchantStore(merchantStoreDto);
        } else {
            return ServerResponse.createByErrorMessage("您没有权限操作该方法");
        }
    }


}
